/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.util.Map;

import lineage.util.collections.Maps;

/**
 * 用于加载本地图片.
 * 
 * @version 2014年2月14日下午1:13:52
 * @author jrwz
 */
public final class L1LoadImage {
    private final Map<String, Image> _list = Maps.newHashMap();
    private Image img;
    /** 图片所在的相对路径名. */
    private final String urlPath = "./image";
    /** 图片的后缀名. */
    private final String suffixName = "png";

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static L1LoadImage instance = new L1LoadImage();
    }

    /**
     * 取得该类的实例.
     */
    public static L1LoadImage getInstance() {
        return Holder.instance;
    }

    private L1LoadImage() {
    }

    /**
     * 加载.图片必须是png格式.
     */
    public void load() {
        File file = new File(urlPath);
        listFile(file, suffixName, true);
    }

    /**
     * 载入指定目录内的所有文件.
     * 
     * @param f
     *            - 图片文件
     * @param suffix
     *            - 后缀名
     * @param isdepth
     *            - 是否遍历子目录
     */
    private void listFile(File f, String suffix, boolean isdepth) {
        // 是目录，同时需要遍历子目录
        if (f.isDirectory() && isdepth) {
            File[] t = f.listFiles();
            for (int i = 0; i < t.length; i++) {
                listFile(t[i], suffix, isdepth);
            }
        } else {
            String filePath = f.getPath(); // getAbsolutePath(绝对路径)
            if (suffix != null) {
                int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引
                String tempsuffix = "";

                if (begIndex != -1) { // 防止是文件但却没有后缀名结束的文件
                    tempsuffix = filePath.substring(begIndex + 1, filePath.length());
                }
                if (tempsuffix.equals(suffix)) {
                    put(filePath);
                }
            }
        }
    }

    /**
     * 将图片放入哈希表.
     * 
     * @param path
     *            - 图片所在的相对目录(含文件全名)
     */
    private void put(String path) {
        String result = path.replaceAll("\\\\", "/"); // 转换斜杠
        img = Toolkit.getDefaultToolkit().createImage(result/* L1LoadImage.class.getResource("1.png").getPath() */);
        _list.put(result, img);
    }

    /**
     * 取得指定的图片.
     * 
     * @param url
     *            - 带后缀的图片名(含相对路径名)
     * @return 哈希表中的对应图片 找不到则返回null
     */
    public Image getImg(String url) {
        if (_list.containsKey(url)) {
            return _list.get(url);
        }
        return null;
    }
}
